/*
 ============================================================================
 Name		: RocketAppView.cpp
 Author	  : Leo.Shi
 Copyright   : Your copyright notice
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include"engine.h"
#include "RocketAppView.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CRocketAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CRocketAppView* CRocketAppView::NewL(const TRect& aRect)
	{
	CRocketAppView* self = CRocketAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CRocketAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CRocketAppView* CRocketAppView::NewLC(const TRect& aRect)
	{
	CRocketAppView* self = new (ELeave) CRocketAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CRocketAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CRocketAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();

	m_arrControl = new (ELeave) CArrayFixFlat<CCoeControl*>(8);
	// Set the windows size
	//SetRect(aRect);
	this->SetExtentToWholeScreen();

	iEngine = new (ELeave)CEngine;
	iEngine->SysView = this;
	iPeriodic = CPeriodic::NewL(0);
	iPeriodic->Start(1000, 200 * 1000, TCallBack(TimerCallBack, this));
	
	
	
	
	
	
	// Activate the window, which makes it ready to be drawn
	ActivateL();
	}

// -----------------------------------------------------------------------------
// CRocketAppView::CRocketAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CRocketAppView::CRocketAppView()
	{
	// No implementation required


	}

// -----------------------------------------------------------------------------
// CRocketAppView::~CRocketAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CRocketAppView::~CRocketAppView()
	{
	// No implementation required
	iPeriodic->Cancel();
	delete iPeriodic;
	delete iEngine;
	delete m_arrControl;
	}

 TInt CRocketAppView::TimerCallBack(TAny* p)
	{
	 CRocketAppView* self = (CRocketAppView*)(p);
	 self->iEngine->SysTimer();	 
	 return 0;
	}

// -----------------------------------------------------------------------------
// CRocketAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CRocketAppView::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();

	iEngine->SysDraw(gc);

	}

// -----------------------------------------------------------------------------
// CRocketAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CRocketAppView::SizeChanged()
	{
	DrawNow();
	}

// -----------------------------------------------------------------------------
// CRocketAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CRocketAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{

	// Call base class HandlePointerEventL()
	CCoeControl*	pControl;
	for (TInt nControl = 0; nControl < m_arrControl->Count(); nControl ++)
	{
		pControl = m_arrControl->At(nControl);
		if (pControl->IsFocused())		
		{
			pControl->HandlePointerEventL(aPointerEvent);
			return;
		}
	}
	
	
	
	iEngine->SysMouseProc(aPointerEvent.iType, aPointerEvent.iModifiers, aPointerEvent.iPosition, aPointerEvent.iParentPosition);
	
	//CCoeControl::HandlePointerEventL(aPointerEvent);
	}

TKeyResponse CRocketAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	//if (IsUpDownKey(aKeyEvent.iScanCode))
	//	return	EKeyWasNotConsumed;

	CCoeControl*	pControl;
	for (TInt nControl = 0; nControl < m_arrControl->Count(); nControl ++)
	{
		pControl = m_arrControl->At(nControl);
		if (pControl->IsFocused())		
			return	pControl->OfferKeyEventL(aKeyEvent, aType);
	}
	
	iEngine->SysKeyProc(aKeyEvent.iModifiers, aKeyEvent.iCode, aKeyEvent.iScanCode, aType);
	
	
	return	EKeyWasNotConsumed;
	}

TInt CRocketAppView::CountComponentControls() const
	{
	return	m_arrControl->Count();
	}

CCoeControl* CRocketAppView::ComponentControl(TInt aIndex) const
	{
	return	m_arrControl->At(aIndex);
	}

void CRocketAppView::PushControl(CCoeControl* pControl )
	{
	m_arrControl->AppendL(pControl);
	}

void CRocketAppView::PopControl(TInt nCount )	//0:for clear all controls
	{
	while (nCount-- && m_arrControl->Count())
		{
			m_arrControl->Delete(m_arrControl->Count() - 1);
		}
	}


// End of File
